package com.cea.papyrus.diagram.composite.helper;

import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.InstanceSpecification;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.Slot;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.helper.EdgeInstanceSpecificationHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;


/**
 * This is the helper used to draw instance specification link in the composite diagram.
 *  In this diagram, the leink have to be draw between 2  slots and not 2 instances specification as the class diagram
 **/
public class CompositeEdgeInstanceSpecificationHelper extends
EdgeInstanceSpecificationHelper {


	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.diagram.classdiagram.helper.EdgeInstanceSpecificationHelper#canCreateGraphElement(com.cea.papyrus.diagraminterchange2.di2.GraphElement)
	 */
	public boolean canCreateGraphElement(GraphElement source){
		Element eltSource=null;
		if (source.getSemanticModel()!=null ){
			if (((Uml1SemanticModelBridge)source.getSemanticModel()).getElement()!=null){
				eltSource=((Uml1SemanticModelBridge)source.getSemanticModel()).getElement();
				if (eltSource instanceof  Package){// they are  classifiers
					return true;
				}
			}
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.diagram.classdiagram.helper.EdgeInstanceSpecificationHelper#createUMLElement(java.lang.String, org.eclipse.uml2.uml.Element)
	 */	
	public  org.eclipse.uml2.uml.Element createUMLElement(String name, org.eclipse.uml2.uml.Element umlParent){
		Debug.debug(this,"create link", channel); //$NON-NLS-1$
		InstanceSpecification is= UMLFactory.eINSTANCE.createInstanceSpecification();
		is.setName(name);
		if(umlParent instanceof Slot){
			((Package)((umlParent.getOwner().getOwner()))).getPackagedElements().add(is);
		}
		else{//instanceSpecification
		((Package)(umlParent.getOwner())).getPackagedElements().add(is);}
		return is;		
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.diagram.classdiagram.helper.EdgeInstanceSpecificationHelper#connect(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	public boolean connect(Element source, Element target, Element link) {
		InstanceSpecification instanceSpec=(InstanceSpecification)link;
		instanceSpec.setName("Link");
		//this is a slot so we have'nt to take the owner this is an instance specification
		//we have to take the owner of the instance specifacation: the owner of the owner
		((Package)((target.getOwner().getOwner()))).getPackagedElements().add(instanceSpec);
		return true;
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.diagram.classdiagram.helper.EdgeInstanceSpecificationHelper#canConnect(org.eclipse.uml2.uml.Element, org.eclipse.uml2.uml.Element)
	 */
	public boolean canConnect(Element source,Element target){
		// if no source or no target found
		if ((source != null) &&(source instanceof Slot) &&(target == null)) {
			return true;
		}
		// Test source and target types
		if ((source instanceof Slot) && (target instanceof Slot))
		{ // they are  instance specifications
			return true;
		}
		return false;
	}
}
